package com.kingyun.gpsinspection.purificationservice.services.jsm.innerservice.impl.teaminfo;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.kingyun.gpsinspection.purificationservice.common.utils.BootstrapUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.jsm.TeamMembersMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.BootstrapSearchBean;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Employee;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.TeamMembers;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.employee.JsmEmployeeService;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.teaminfo.JsmTeamMembersService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * Created by zmling on 2016/12/14.
 * 班组成员信息
 */

public class JsmTeamMembersServiceImpl implements JsmTeamMembersService {

    @Autowired
    private TeamMembersMapper teamMembersMapper;

    @Autowired
    private JsmEmployeeService employeeService;

    /**
     * 新增班组成员信息
     *
     * @param teamMembers
     * @return
     * @throws Exception
     */
    @Override
    public Map<String, Object> addTeamMember(TeamMembers teamMembers,Employee employee) throws Exception {
        Map<String, Object> map = new HashMap<>();
        List<String> strList = new ArrayList<String>(Arrays.asList(teamMembers.getEmployeeId().split(",")));
        teamMembers.setStrList(strList);
        List<TeamMembers> list = teamMembersMapper.findIsExist(teamMembers);
        if (list != null && list.size() > 0 && strList.size() > 0) {
            map.put("rest", 1);
            map.put("msg", "你添加的班组人员已经存在！！");
        } else {
            List<TeamMembers> tList = new ArrayList<TeamMembers>();
            Date date = new Date();
            String userId = employee.getEmployeeId();
            for (String str : strList) {
                TeamMembers param = new TeamMembers();
                param.setTeamId(teamMembers.getTeamId());
                param.setTeamMembersId(UniqueUtil.uuid());
                param.setEmployeeId(str);
                param.setJobRole(teamMembers.getJobRole());
                param.setStatus((short) 0);
                param.setCreatorId(userId);
                param.setCreateDate(date);
                tList.add(param);
            }
            teamMembersMapper.insertList(tList);
//            teamMembersMapper.insertSelective(teamMembers);
            map.put("rest", "0");
            map.put("msg", "新增成功！");
        }
        return map;
    }

    /**
     * 班组信息分页查询
     *
     * @param bootstrapSearchBean
     * @return
     * @throws Exception
     */
    @Override
    public JSONObject findTeamMemberId(BootstrapSearchBean bootstrapSearchBean) throws Exception {
        JSONObject jsonObject = new JSONObject();
        bootstrapSearchBean.setLimit(bootstrapSearchBean.getLimit() == null ? 10 : bootstrapSearchBean.getLimit());
        bootstrapSearchBean.setOffset(bootstrapSearchBean.getOffset() == null ? 0 : bootstrapSearchBean.getOffset());
        bootstrapSearchBean.setFilterMap(BootstrapUtil.filterStringToMap(bootstrapSearchBean.getFilter()));
        if (bootstrapSearchBean.getSearch() != null && bootstrapSearchBean.getSearch() != "") {
            bootstrapSearchBean.setSearch(bootstrapSearchBean.getSearch().trim());
        }
        jsonObject.put("total", teamMembersMapper.queryCount(bootstrapSearchBean));
        jsonObject.put("rows", JSON.toJSON(teamMembersMapper.queryList(bootstrapSearchBean)));
        return jsonObject;
    }

    /**
     * 查询
     *
     * @param teamId
     * @param employeeId
     * @return
     */
    @Override
    public List<TeamMembers> findTeamMembers(String teamId, String employeeId) {
        TeamMembers te = new TeamMembers();
        te.setTeamId(teamId);
        te.setEmployeeId(employeeId);
        return teamMembersMapper.findTeamMembers(te);
    }

    /**
     * 查询
     *
     * @param teamIds
     * @return
     * @throws Exception
     */
    @Override
    public int deleteTeam(Map teamIds) throws Exception {
        return teamMembersMapper.updateTeamByIds(teamIds);
    }

    /**
     * 查询
     *
     * @param teamMembersId
     * @return
     * @throws Exception
     */
    @Override
    public TeamMembers findByteamMembersId(String teamMembersId) throws Exception {
        return teamMembersMapper.selectByPrimaryKey(teamMembersId);
    }

    /**
     * 查询班组成员信息
     *
     * @param record
     * @return
     */
    @Override
    public List<TeamMembers> selectByParamList(TeamMembers record) {
        return teamMembersMapper.selectByParamList(record);
    }

    /**
     * 修改
     *
     * @param teamIds
     * @return
     * @throws Exception
     */
    @Override
    public int updateTeamId(Map teamIds) throws Exception {
        return teamMembersMapper.updateTeamId(teamIds);
    }

    /**
     * 根据人员信息ID查询成员信息
     *
     * @param employeeId
     * @return
     */
    @Override
    public TeamMembers selectByEmployeeId(String employeeId) {
        return teamMembersMapper.selectByEmployeeId(employeeId);
    }

}
